#!/usr/bin/env python

import sys
import colorama

from _app import App


class Console(App):
    """
    Base class for CLI application.
    """

    _arg = None
    _silent = False
    _verbose = False

    def __init__(self, arg):
        super(Console, self).__init__()

        self._arg = arg

        try:
            self._verbose = arg.verbose
        except AttributeError:
            self._verbose = False

        try:
            self._silent = arg.silent
        except AttributeError:
            self._silent = False
        if self._silent:
            return

        colorama.init(autoreset=True)

    def __del__(self):
        if self._silent:
            return

        colorama.deinit()

    @property
    def argument(self):
        """
        Argument supplied to application.

        @rtype: Argument
        @return: Argument object.
        """

        return self._arg

    def fuss(self, message, method='yell'):
        """
        Print text when in verbose mode. Useful for debugging.

        Method valid value:
            - yell
            - cheer
            - progress
            - alert
            - warn
            - flunk

        @param message:
        @param method: The method.

        @rtype : None
        @return: None
        """

        if self._verbose:
            if message:
                getattr(self, method)(message)

    def yell(self, message):
        """
        Print text in bright white color unless silent mode activated.

        @type message: str
        @param message: Message to show to user.
        @rtype : None
        @return: None
        """

        if self._silent:
            return

        message = '{0}'.format(message)
        sys.stdout.write(colorama.Fore.WHITE + colorama.Style.BRIGHT + message)

    def cheer(self, message):
        """
        Print text in bright green color unless silent mode activated.

        @type message: str
        @param message: Message to show to user.
        @rtype : None
        @return: None
        """

        if self._silent:
            return

        message = '{0}'.format(message)
        sys.stdout.write(colorama.Fore.GREEN + colorama.Style.BRIGHT + message)

    def progress(self, message):
        """
        Print text in bright cyan color unless silent mode activated.

        @type message: str
        @param message: Message to show to user.
        @rtype : None
        @return: None
        """

        if self._silent:
            return

        message = '{0}'.format(message)
        sys.stdout.write(colorama.Fore.CYAN + colorama.Style.BRIGHT + message)

    def alert(self, message):
        """
        Print text in bright magenta color unless silent mode activated.

        @type message: str
        @param message: Message to show to user.
        @rtype : None
        @return: None
        """

        if self._silent:
            return

        s = '{0}'.format(message)
        sys.stdout.write(colorama.Fore.MAGENTA + colorama.Style.BRIGHT + s)

    def warn(self, message):
        """
        Print text in bright yellow color unless silent mode activated.

        @type message: str
        @param message: Message to show to user.
        @rtype : None
        @return: None
        """

        if self._silent:
            return

        message = '{0}'.format(message)
        sys.stdout.write(colorama.Fore.YELLOW + colorama.Style.BRIGHT + message)

    def flunk(self, message):
        """
        Print text in bright red color unless silent mode activated.

        @type message: str
        @param message: Message to show to user.
        @rtype : None
        @return: None
        """

        if self._silent:
            return

        message = '{0}'.format(message)
        sys.stdout.write(colorama.Fore.RED + colorama.Style.BRIGHT + message)

    @staticmethod
    def ask(message):
        """
        Print text in bright white color and wait for user input.

        Answer that begin with Y or y considered yes, otherwise no.

        @param message:
        @rtype: bool
        @return: Answer to yes or no.
        """

        s = colorama.Fore.WHITE + colorama.Style.BRIGHT + '%s [yes|no] '
        return raw_input(s % message)[0].lower() == 'y'
